
//	------------------------------------------------------------------------------
//                                       _                          
//              _ __ ___   ___ _ __ ___ | |__  _ __ __ _ _ __   ___ 
//             | '_ ` _ \ / _ \ '_ ` _ \| '_ \| '__/ _` | '_ \ / _ \
//             | | | | | |  __/ | | | | | |_) | | | (_| | | | |  __/
//             |_| |_| |_|\___|_| |_| |_|_.__/|_|  \__,_|_| |_|\___|
//                                                     
//	------------------------------------------------------------------------------

#region - License -
//
//	------------------------------------------------------------------------------                                                   
//	         Copyright (c) 2010 Peter Vyvey (peter.vyvey@chiliware.be)
//	------------------------------------------------------------------------------
//
//
//	This file is part of CHILIWARE MEMBRANE.
//
//	CHILIWARE MEMBRANE is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CHILIWARE MEMBRANE is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU Lesser General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public License
//	along with CHILIWARE MEMBRANE.  If not, see <http://www.gnu.org/licenses/>.
//
//
#endregion

#region - Changes -
//
//	peter.vyvey@chiliware.be		2010-08-09		Created
//
#endregion

#region - Using -

using System.Configuration;
using System.Web.Configuration;
using Chiliware.Membrane.ExceptionManagement;

#endregion

namespace Chiliware.Membrane.Configuration
{
	#region - WorkspaceConfiguration -

	/// <summary>
	/// Represents the workspace configuration.
	/// </summary>
	public static class WorkspaceConfiguration
	{
		#region - Constants & static fields -

		/// <summary>
		/// Key of the APPLICATION MODEL node in the conffiguration file.
		/// </summary>
		private const string APPLICATION_MODEL = "applicationModel";

		/// <summary>
		/// Template to format the configuration filename.
		/// </summary>
		private const string CONFIGURATION_FILE_TEMPLATE = "{0}.config";

		/// <summary>
		/// The configuration file name in a web environment.
		/// </summary>
		private const string WEB_CONFIGURATION_FILE_NAME = "~/web.config";

		#endregion

		#region - Static fields -

		/// <summary>
		/// Backingstore field for the ConfigurationManager property.
		/// </summary>
		private static System.Configuration.Configuration configurationManager = null;

		/// <summary>
		/// Backingstore field for the ConfigurationFileMap property.
		/// </summary>
		private static ExeConfigurationFileMap configurationFileMap;

		/// <summary>
		/// Backingstore field for the Model property.
		/// </summary>
		private static ConfigurationModel model = null;

		#endregion
		
		#region - Static properties -

		/// <summary>
		/// Reference to the workspace model configuration. 
		/// </summary>
        public static System.Configuration.Configuration ConfigurationManager
        {
            get
            {
                try
                {
					configurationManager = WebConfigurationManager.OpenWebConfiguration(WorkspaceConfiguration.WEB_CONFIGURATION_FILE_NAME);
                }
                catch
                {
                    try
                    {
                        configurationManager = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(WorkspaceConfiguration.ConfigurationFileMap, ConfigurationUserLevel.None);
                    }
                    catch
                    {
						throw new ApplicationModelConfigurationException(ExceptionNumber.MissingConfigurationFile);
                    }
                }

                return configurationManager;
            }
        }

		/// <summary>
		/// Reference to the configuration file map.
		/// </summary>
		public static ExeConfigurationFileMap ConfigurationFileMap
		{
			get
			{
				if (configurationFileMap == null)
				{
					configurationFileMap = new ExeConfigurationFileMap();
					configurationFileMap.ExeConfigFilename = string.Format(WorkspaceConfiguration.CONFIGURATION_FILE_TEMPLATE, System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
				}

				return configurationFileMap;
			}
		}

		/// <summary>
		/// The configuration model.
		/// </summary>
		public static ConfigurationModel Configuration
		{
			get 
			{
				try
				{
					if (WorkspaceConfiguration.model == null)
					{
						WorkspaceConfiguration.model = (ConfigurationModel)ConfigurationManager.GetSection(WorkspaceConfiguration.APPLICATION_MODEL);
					}
				}
				catch { /* IGNORE */ }

				return WorkspaceConfiguration.model;
			}
			set { WorkspaceConfiguration.model = value; }
		}

		#endregion
	}

	#endregion
}